package org.jmetrix.monitor.impl;

import org.jmetrix.api.Aggregator;
import org.jmetrix.api.Measurement;
import org.jmetrix.bus.Listener;
import org.jmetrix.bus.impl.NamedBusFactory;
import org.jmetrix.monitor.Monitor;

/**
 * @author Konstantin Kuzmin
 * @version $Revision$
 * @since 02.10.11
 */
public class MonitorImpl implements Monitor {
    private Aggregator<Measurement> metrixAggregator;

    protected void setMetrixAggregator(final Aggregator metrixAggregator) {
        this.metrixAggregator = metrixAggregator;
    }

    protected Aggregator<Measurement> getMetrixAggregator() {
        return metrixAggregator;
    }

    public MonitorImpl() {

    }

    public MonitorImpl(final Aggregator metrixAggregator) {
        setMetrixAggregator(metrixAggregator);
    }

    @Override
    public synchronized void start() {
        NamedBusFactory.getDefaultBus().addListener(new Listener<Measurement>() {
            @Override
            public void onEvent(final Measurement event) {
                metrixAggregator.aggregate(event);
            }
        });
    }

    @Override
    public void stop() {
        //TODO detach listener
    }
}
